package 复习.面试TOP101.链表;

import java.util.ArrayList;
import java.util.PriorityQueue;

public class 合并K个已排序的链表 {
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        PriorityQueue<ListNode> q = new PriorityQueue<>((q1, q2) -> (q1.val - q2.val));
        for(int i = 0;i < lists.size();i++){
            if(lists.get(i) != null) q.offer(lists.get(i));
        }
        ListNode node = new ListNode(-1);
        ListNode head = node;
        while(!q.isEmpty()){
            ListNode tmp = q.poll();
            head.next = tmp;
            head = head.next;
            if(tmp.next != null) q.offer(tmp.next);
        }
        return node.next;
    }
}
